package com.sunlg.smarthome.ui.main;

import android.support.design.widget.TextInputEditText;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.BaseActivity;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.presenter.LoginPresenter;
import com.sunlg.smarthome.contract.LoginContract;
import com.sunlg.smarthome.utils.PreferHelper;

import butterknife.BindView;

public class LoginActivity extends BaseActivity
        implements LoginContract.View{

    @BindView(R.id.et_username)
    TextInputEditText etUserName;

    @BindView(R.id.et_password)
    TextInputEditText etPassword;

    @BindView(R.id.cb_is_remember)
    CheckBox cbIsRemember;

    @BindView(R.id.cb_is_auto_login)
    CheckBox cbIsAutoLogin;

    @BindView(R.id.btn_login)
    Button btnLogin;

    @BindView(R.id.ll_new_user)
    LinearLayout llNewUser;

    private LoginContract.Presenter presenter;
    private PreferHelper mPreferHelper;

    private String username;
    private String password;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        presenter = new LoginPresenter();
        presenter.attachView(this);

        mPreferHelper = App.getInstance().getPreferHelper();
    }

    @Override
    protected void initView() {
        if(mPreferHelper.getBoolean(C.PreKey.IS_REMEMBER, false)){
            etUserName.setText(mPreferHelper.getString(C.PreKey.USER_NAME, ""));
            etPassword.setText(mPreferHelper.getString(C.PreKey.PASSWORD, ""));
            cbIsRemember.setChecked(true);
        }
        if(mPreferHelper.getBoolean(C.PreKey.IS_AUTO_LOGIN, false)){
            etUserName.setText(mPreferHelper.getString(C.PreKey.USER_NAME, ""));
            etPassword.setText(mPreferHelper.getString(C.PreKey.PASSWORD, ""));
            cbIsAutoLogin.setChecked(true);
        }

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = etUserName.getText().toString();
                password = etPassword.getText().toString();
                if(!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password))
                    presenter.login(username,password);
                else
                    toast("账号或密码不能为空");
            }
        });

        llNewUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overlay(RegisterActivity.class);
            }
        });

        //是否记住密码复选框监听器
        cbIsRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mPreferHelper.setBoolean(C.PreKey.IS_REMEMBER, b);
            }
        });

        //是否自动登录复选框监听器
        cbIsAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mPreferHelper.setBoolean(C.PreKey.IS_AUTO_LOGIN, b);
            }
        });
    }

    @Override
    protected void setupToolBar(Toolbar toolbar) {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setTitle("用户登录");
    }

    @Override
    public void showLoading() {
        showLoadBar("正在登录...");
    }

    @Override
    public void hideLoading() {
        hideLoadBar();
    }

    @Override
    public void forward() {
        forward(MainActivity.class);
    }

    @Override
    public void showError(String msg) {
        toast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.dettachView();
    }
}
